package cn.tedu.xuecha.respone;

import cn.tedu.xuecha.ex.ServiceException;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author coinyang
 * @date 2023/4/22 14:50
 */
@Data
public class JsonResult {

    /**
     * 操作结果的状态码
     */
    @ApiModelProperty("业务状态码")
    private Integer state;
    /**
     * 操作“失败”时响应的提示文本
     */
    @ApiModelProperty("消息")
    private String message;
    /**
     * 操作“成功”时响应的数据
     */
    @ApiModelProperty("数据")
    private Object data;

    public static JsonResult ok() {
        return ok(null);
    }

    public static  JsonResult ok(Object data) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(AppCode.OK.getValue());
        jsonResult.setData(data);
        return jsonResult;
    }

    public static JsonResult fail(ServiceException e) {
        return fail(e.getAppCode(), e.getMessage());
    }

    public static JsonResult fail(AppCode serviceCode, String message) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(serviceCode.getValue());
        jsonResult.setMessage(message);
        return jsonResult;
    }

}
